package idiet.dao.hibernate;

import idiet.bean.Customer;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("customerDao")
public class CustomerDao extends BaseDao{
	public Customer getCustomerByCustomerId(int customerId){
		return (Customer) this.sessionFactory
				.openSession()
				.createCriteria(Customer.class)
				.add(Restrictions.eq("food_id", customerId))
				.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<Customer> getCustomersByCustomerName(String customerName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Customer.class)
				.add(Restrictions.like("name", customerName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<Customer> getCustomers(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Customer.class)
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<Customer> getCustomers(String customerName,Date lmTimestampStart,Date lmTimestampEnd){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Customer.class)
				.add(Restrictions.between("lmTimestamp", lmTimestampStart, lmTimestampEnd))
				.add(Restrictions.like("name", customerName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<Customer> getCustomers(Timestamp lmTimestampStart,Timestamp lmTimestampEnd){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Customer.class)
				.add(Restrictions.between("lmTimestamp", lmTimestampStart, lmTimestampEnd))
				.list();
	}
	public void createCustomer(Customer customer){
		this.sessionFactory
		.getCurrentSession()
		.save(customer);
	}
	public void deleteCustomer(Customer customer){
		this.sessionFactory
		.getCurrentSession()
		.delete(customer);
	}
	public void updateCustomer(Customer customer){
		this.sessionFactory
		.getCurrentSession()
		.update(customer);
	}
}
